package TUILayer;//makes this class a part of the TUILayer package
import ControlLayer.*;
import ModelLayer.*;
import java.util.*;

/**
 * 
 * User interface to the AddressBook Controller
 * <p>
 * <code>AddressBookUI</code> class manages information about friends and passes input data to the <code>ControlLayer</code>.
 * <br>
 * @author (dmaj0913 - group 2)
 * @version (2013.11.08)
 * <p>
 * <b><i>History:</i></b>
 * <p>
 * (KBH): 2005.03.31:
 * <p>
 * (FEN): 2006.03.21: Comments translated into English
 * <p>
 * (FEN): 2006.03.21: Some changes to the architecture in order to assure layering and loose coupling
 * 
 */
public class AddressBookUI
{   
    private AddressCtr addrCtr;
    private AddressBook addressMemoryBackup;
    private boolean existing;
    /**
     * Constructor creates new instance of <code>AddressBook</code> class.
     */
    public AddressBookUI()
    {
       addressMemoryBackup = new AddressBook();
       existing = false;
    }
    
    /**
     * Starts the User Interface of the AddressBook controller(calls <code>AddressMenu</code> method) and returns a backup of collection of the AddressBook.
     * @param backup keeps a backup of previous AddressBook.
     * @return backup of the modified AddressBook.
     */
    public AddressBook start(AddressBook backup)
    {
        addressMemoryBackup = backup;
        addrCtr=new AddressCtr(backup);
        if(addressMemoryBackup.getPersons().size() > 0) existing = true;
        addressMenu();
        return addressMemoryBackup;
    }
    
    /**
     * Manages the data flow in the terminal and passes information to the other layers.
     */
    public void addressMenu()
    {
        Scanner keyboard = new Scanner(System.in); 
        boolean exit = false;
        while(!exit)
        {
            int choice = writeAddressBookMenu(); 
            if(choice == 1)
            {
                if(existing == true)
                {   
                    getPerson();
                    keyboard.nextLine();
                }
            }
            else
            {
                if(choice == 2)
                {
                    //create person
                    long id=inputPersonsID();
                    while(addressMemoryBackup.getPerson(id) != null)
                    {
                        System.out.println("The ID you entered is invalid, please try again: ");
                        id = inputPersonsID();
                    }
                    if (!addrCtr.isPerson(id))
                    {
                        String name=inputName();
                        String address=inputAddress();
                        String postal=inputPostalCode();
                        String city=inputCity();
                        String tel=inputPhone();
                        addrCtr.createPerson(id,name,address,postal,city,tel);
                        existing=true;
                    }
                }  
                else
                {
                    if(choice == 3)
                    {
                    if(existing == true)
                    {
                        //delete person
                        long id=inputPersonsID();
                        while(!addrCtr.isPerson(id))
                        {
                            System.out.println("The ID you entered is invalid, please try again: ");
                            id = inputPersonsID();
                        }
                        addrCtr.deletePerson(id);
                    }
                    }
                    else
                    {
                      if(choice == 4)
                      {
                      if(existing == true)
                      {
                          // update person
                          long id=inputPersonsID();
                          if(addrCtr.isPerson(id))
                          {
                              String name=inputName();
                              String address=inputAddress();
                              String postal=inputPostalCode();
                              String city=inputCity();
                              String tel=inputPhone();
                              addrCtr.updatePerson(id,name,address,postal,city,tel);
                          }
                      }
                    }
                      else
                      {
                          if(choice == 5)
                          {
                          if(existing == true)
                          {
                              // listAllPersons();
                              addrCtr.listAllPersons();
                              keyboard.nextLine();
                            }
                        }
                          else
                          {
                              exit = true;
                          }//end else
                      }//end else
                    }//end else
                }//end else
            }//end else
            
       }//end while
    }
    
    /**
     * Accepts input data and prints information in the Terminal.
     * @return number of type <code>Int<code> as a desired user choice.
     */ 
    public int writeAddressBookMenu()
    { 
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** AddressBook Menu ***");
        System.out.println(" (1) Show information about person");
        System.out.println(" (2) Add person");
        System.out.println(" (3) Delete person");
        System.out.println(" (4) Update person");
        System.out.println(" (5) List all persons");
        System.out.println(" (6) Return");
        if(existing == false) System.out.println("NOTE: There are no people added, please add one first before proceeding");
        System.out.print("\n Make your choice: ");
        int choice = keyboard.nextInt();
        return choice;
    }
    
    /**
     * Asks the user for entering desired id in the Terminal.
     * @return number of type <code>long</code> that is used as id of a friend.
     */
    public long inputPersonsID()
    {   
        boolean ok = false;
        long id = 0;
        while(!ok)
        {
            Scanner keyboard = new Scanner(System.in);
            System.out.println(" Write Id:  ");
            try
            {
                id = keyboard.nextLong();
                ok = true;
            }
            catch (Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }
        return id;
    }
    
    /**
     * Asks the user for entering desired Name in the Terminal.
     * @return Character String that is used as Name of a friend.
     */
    public String inputName()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Name:  ");
         String name = keyboard.nextLine();
         return name;
    }
    
    /**
     * Asks the user for entering desired Postal Code in the Terminal.
     * @return Character String that is used as Postal Code of a friend.
     */
    public String inputPostalCode()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Postal Code:  ");
         String code = keyboard.nextLine();
         return code;
    }
    
    /**
     * Asks the user for entering desired City in the Terminal.
     * @return Character String that is used as City a friend is living in.
     */
    public String inputCity()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write City:  ");
         String city = keyboard.nextLine();
         return city;
    }
    
    /**
     * Asks the user for entering desired Phone Number in the Terminal.
     * @return Character String that is used as Phone Number of a friend.
     */
    public String inputPhone()
    {
         // makes an object keyboard to have input from the screen
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write Phone Number:  ");
         String phone = keyboard.nextLine();
         return phone;
    } 
    
    /**
     * Asks the user for entering desired Address in the Terminal.
     * @return Character String that is used as Address of a friend.
     */
    public String  inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }
    
    /**
     * Prints out in the Terminal the ID of a friend.
     * @param id stores the id of a friend that should be printed out.
     */
    public void showId(long id)
    {
        System.out.println("ID of the person: " + id);
    }
    
    /**
     * Prints out in the Terminal the Name of a friend.
     * @param name stores the name of a friend that should be printed out.
     */
    public void showName(String name)
    { 
        System.out.println("The name of the person: " + name);
    }
    
    /**
     * Prints out in the Terminal the Address of a friend.
     * @param name string variable that stores the name of a friend that should be printed out.
     */
    public void showAddress(String address)
    { 
        System.out.println("The address of the person: " + address);
    }
    
    /**
     * Prints out in the Terminal all the infromation about a friend by entering an ID.
     */
    private void getPerson()
    {
        long id = inputPersonsID();
        addrCtr.getPerson(id,true);

    }   
}
            
        
